package com.example.locationservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import com.example.locationservice.database.DBHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SqliteActivity extends AppCompatActivity {
    private static String TAG = "SQLite测试";
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);
         //初始化数据库
        dbHelper = new DBHelper(SqliteActivity.this, "location.db", null, 1);
        SQLiteDatabase database=dbHelper.getWritableDatabase();
        insert(database);

        query(database);
        delete(database);
        update(database);
        query(database);
        //关闭数据库连接
        database.close();
    }

    private void insert(SQLiteDatabase database) {
        //把数据添加到ContentValues
        ContentValues values = new ContentValues();
        values.put("lat", 35.6);
        values.put("lng",121.55);
        values.put("altitude", 35);
        values.put("accuracy", 35);

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        values.put("time", dateNowStr);

        //添加数据到数据库
        long index = database.insert("result", null, values);

        //大于0表示添加成功
        if (index > 0) {
            Log.e(TAG,"插入成功");
        } else {
            Log.e(TAG,"插入失败");
        }
    }

    private void query(SQLiteDatabase database) {
        Cursor cursor =database.rawQuery("select * from result", null);
        if (cursor.getCount() != 0) {
            cursor.moveToFirst();
            while(!cursor.isAfterLast()) {
                Log.e("id", String.valueOf(cursor.getInt(0)));
                Log.e("lat", String.valueOf(cursor.getDouble(1)));
                Log.e("lng", String.valueOf(cursor.getDouble(2)));
                Log.e("altitude", String.valueOf(cursor.getDouble(3)));
                Log.e("accuracy", String.valueOf(cursor.getDouble(4)));
                Log.e("time", cursor.getString(5));
                cursor.moveToNext();
            }
            cursor.close();
        }
    }

    private void delete(SQLiteDatabase database) {

        long index = database.delete("result", "id = ?",new String[]{"1"});

        //大于0表示添加成功
        if (index > 0) {
            Log.e(TAG,"删除成功");
        } else {
            Log.e(TAG,"删除失败");
        }
    }
    private void update(SQLiteDatabase database) {

        //把数据添加到ContentValues
        ContentValues values = new ContentValues();
        values.put("lat", 35.6);
        values.put("lng",121.55);
        values.put("altitude", 355555);
        values.put("accuracy", 35);

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        values.put("time", dateNowStr);

        //添加数据到数据库
        long index = database.update("result", values, "id=?",new String[]{"2"});

        //大于0表示添加成功
        if (index > 0) {
            Log.e(TAG,"更新成功");
        } else {
            Log.e(TAG,"更新失败");
        }
    }
}